//
//  WWPayOrderViewController.m
//  WorthWhile
//
//  Created by Mac on 2018/5/14.
//  Copyright © 2018年 stoneobs.icloud.com. All rights reserved.
//

#import "BLHotlePayViewController.h"
#import "WWPayChosedControl.h"
@interface BLHotlePayViewController ()
@property(nonatomic, strong) WWPayChosedControl                     *aliChosedControl;/**< 阿里 */
@property(nonatomic, strong) WWPayChosedControl                     *weChatChosedControl;/**< 微信 */
@end

@implementation BLHotlePayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提交订单";
    [self configSubView];
    [self configTableFooterView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 122)];
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 170)];
    imageView.image = [UIImage imageNamed:@"dingdan"];
    [header addSubview:imageView];

    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 15, UIScreenWidth, 40)
                                                     text:self.moneySting
                                                textColor:UIColor.whiteColor
                                                     font:25
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.centerY = header.height/2;
    [header addSubview:titleLable];
    
    
    STLabel * orderLable = [[STLabel alloc] initWithFrame:CGRectMake(0, titleLable.bottom, UIScreenWidth, 40)
                                                     text:[NSString stringWithFormat:@"订单号:%@",
                                                           self.orderNum]
                                                textColor:UIColor.whiteColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];

    [header addSubview:orderLable];
    
    STLabel * desLable = [[STLabel alloc] initWithFrame:CGRectMake(0, orderLable.bottom, UIScreenWidth, 40)
                                                     text:self.desString
                                                textColor:UIColor.whiteColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    
    [header addSubview:desLable];
    
    header.height = imageView.bottom;
     self.tableView.tableHeaderView = header;
    
}
- (void)configTableFooterView{
    __weak typeof(self) weakSelf =  self;
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 121)];
    header.backgroundColor = self.tableView.backgroundColor;
    
    
    self.weChatChosedControl = [WWPayChosedControl controlWithImageName:@"微信支付" title:@"微信支付" handle:^(WWPayChosedControl *control) {
        [weakSelf onSelctedChosedButton:control];
    }];
    self.weChatChosedControl.top = 10;
    self.weChatChosedControl.selected = YES;
    self.weChatChosedControl.backgroundColor = UIColor.whiteColor;
    [header addSubview:self.weChatChosedControl];
    
    self.aliChosedControl = [WWPayChosedControl controlWithImageName:@"支付宝" title:@"支付宝支付" handle:^(WWPayChosedControl *control) {
        [weakSelf onSelctedChosedButton:control];
    }];
    self.aliChosedControl.backgroundColor = UIColor.whiteColor;
    self.aliChosedControl.top = self.weChatChosedControl.bottom;
    [header addSubview:self.aliChosedControl];
    
   

    
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(0, self.aliChosedControl.bottom + 40, UIScreenWidth  , 49)
                                                        title:@"支付"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:0
                                              backgroundColor:BL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedPayButton];
    }];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    [header addSubview:confimButton];
    
   
     header.height = confimButton.bottom;

    
    self.tableView.tableFooterView = header;
}

#pragma mark --Action Method
- (void)st_leftBarAction:(id)sender{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)onSelctedPayButton{
    if (!self.weChatChosedControl.selected) {
        [self sendPayWithTypeRequest:@"1"];
    }else{
         [self sendPayWithTypeRequest:@"2"];
    }
   
}
- (void)onSelctedChosedButton:(WWPayChosedControl*)control{
    self.weChatChosedControl.selected = NO;
    self.aliChosedControl.selected = NO;
    control.selected = YES;
}


#pragma mark --NetWork Method
//1支付宝 2微信
- (void)sendPayWithTypeRequest:(NSString*)type{
    if (type.integerValue == 1) {
        [self sendAlipayRequest];
    }else{
        [self sendWechatRequest];
    }
    
}
- (void)sendAlipayRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/api/online/alipay"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
     [dic setObject:self.orderNum forKey:@"order"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 //订单号
                                                 NSString * signKey = responseObject[@"data"];
                                                 [TMAliPayManger.manger startPayOrder:signKey handle:^(BOOL success, NSString *alert) {
                                                     if (success) {
                                                         [self.navigationController popToRootViewControllerAnimated:YES];
                                                     }
                                                 }];

                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                             }];
}
- (void)sendWechatRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/api/weipay/Wechat"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [dic setObject:self.orderNum forKey:@"order"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 //订单号
                                                 NSDictionary * wechatDic = responseObject[@"data"];
                                                 [TMWechatSocietyManger.manger startPayOrderDic:wechatDic payHandle:^(BOOL success, NSString *alert) {
                                                     if (success) {
                                                         [self.navigationController popToRootViewControllerAnimated:YES];
                                                     }
                                                 }];
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                             }];
}
@end
